{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6af6c0cc",
   "metadata": {},
   "source": [
    "## 安装Anaconda\n",
    "\n",
    "https://www.anaconda.com/\n",
    "\n",
    "https://www.anaconda.com/products/distribution#Downloads"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7da49e00",
   "metadata": {},
   "source": [
    "## 创建虚拟环境 py3.8\n",
    "\n",
    "**打开 Anaconda Prompt。**\n",
    "\n",
    "### py3.8 with cudatoolkit=11.3\n",
    "\n",
    "\n",
    "```\n",
    "conda create -n py3.8 python=3.8\n",
    "conda activate py3.8\n",
    "conda install cudatoolkit=11.3 cudnn=8.2\n",
    "```\n",
    "### 安装 tensorflow\n",
    "https://tensorflow.google.cn/install/source_windows#gpu\n",
    "\n",
    "推荐使用pip\n",
    "```\n",
    "pip install tensorflow==2.6.0\n",
    "pip install tensorflow==2.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
    "```\n",
    "或者使用conda\n",
    "```\n",
    "conda install tensorflow-gpu==2.6.0\n",
    "```\n",
    "### 安装 pytorch\n",
    "https://pytorch.org/get-started/locally/\n",
    "\n",
    "https://pytorch.org/get-started/previous-versions/\n",
    "\n",
    "torchvision，torchaudio可以不用安装\n",
    "```\n",
    "conda install pytorch==1.10.1 torchvision==0.11.2 torchaudio==0.10.1 cudatoolkit=11.3 -c pytorch -c conda-forge\n",
    "```\n",
    "### 安装其他一些库\n",
    "有时候用conda安装会出现问题，可以使用pip\n",
    "\n",
    "```\n",
    "conda install numpy pandas matplotlib scipy scikit-learn pillow opencv\n",
    "pip install numpy pandas matplotlib scipy scikit-learn pillow opencv-python\n",
    "\n",
    "```\n",
    "### 安装 deepxde\n",
    "```\n",
    "conda install tensorflow-probability=0.14.0\n",
    "conda install -c conda-forge deepxde\n",
    "```\n",
    "\n",
    "### 安装 jupyter\n",
    "```\n",
    "conda install jupyter ipykernel\n",
    "python -m ipykernel install --user --name py3.8 --display-name \"py3.8\"\n",
    "```\n",
    "\n",
    "### 安装 keras\n",
    "如果 import tensorflow 出现no module name keras, 安装 keras\n",
    "```\n",
    "conda install keras=2.6.0\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "92b5ef3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import keras"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f5e68b9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.6.0'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "keras.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1941b78d",
   "metadata": {
    "tags": []
   },
   "source": [
    "### 测试\n",
    "\n",
    "```python\n",
    "python\n",
    ">>> import tensorflow as tf\n",
    ">>> tf.config.list_physical_devices('GPU')\n",
    "[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]\n",
    "\n",
    ">>> import torch\n",
    ">>> print(\"CUDA Available: \",torch.cuda.is_available())\n",
    "CUDA Available:  True\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c775696b",
   "metadata": {
    "tags": []
   },
   "source": [
    "### 启动jupyter notebook\n",
    "\n",
    "先激活py3.8虚拟环境，然后cd代码目录，使用jupyter notebook命令启动\n",
    "\n",
    "```\n",
    "conda activate py3.8\n",
    "E:\n",
    "cd E:\\noetbook\n",
    "jupyter notebook\n",
    "```\n",
    "\n",
    "\n",
    "### 或者启动jupyter lab\n",
    "\n",
    "安装jupyterlab\n",
    "```\n",
    "conda install -c conda-forge jupyterlab\n",
    "```\n",
    "\n",
    "先激活py3.8虚拟环境，然后cd代码目录，使用jupyter lab命令启动\n",
    "\n",
    "```\n",
    "conda activate py3.8\n",
    "E:\n",
    "cd E:\\noetbook\n",
    "jupyter lab\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33913469",
   "metadata": {},
   "source": [
    "## 安装cpu环境\n",
    "\n",
    "如果你没有GPU,可以安装CPU环境。\n",
    "\n",
    "https://tensorflow.google.cn/install/source_windows#cpu\n",
    "\n",
    "https://pytorch.org/get-started/previous-versions/\n",
    "\n",
    "### py3.6-cpu\n",
    "\n",
    "```\n",
    "conda create -n py3.6 python=3.6\n",
    "conda activate py3.6\n",
    "\n",
    "conda install tensorflow==1.15.0  tensorflow-estimator==1.15.1\n",
    "conda install pytorch==1.10.1 torchvision==0.11.2 torchaudio==0.10.1 cpuonly -c pytorch\n",
    "conda install pillow matplotlib scipy  pandas scikit-learn opencv\n",
    "conda install jupyter ipykernel\n",
    "python -m ipykernel install --user --name py3.6 --display-name \"py3.6\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35b07a27",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c58c387f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "2c932aba",
   "metadata": {},
   "source": [
    "## 运行版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9acc9cbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "sys.version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5c216696",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.6.0'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tensorflow\n",
    "\n",
    "tensorflow.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ea0eaa72",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.10.1'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "torch.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b29c7087",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using backend: tensorflow.compat.v1\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From G:\\Anaconda3\\envs\\py3.8\\lib\\site-packages\\tensorflow\\python\\compat\\v2_compat.py:101: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "non-resource variables are not supported in the long term\n",
      "WARNING:tensorflow:From G:\\Anaconda3\\envs\\py3.8\\lib\\site-packages\\deepxde\\nn\\initializers.py:118: The name tf.keras.initializers.he_normal is deprecated. Please use tf.compat.v1.keras.initializers.he_normal instead.\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'1.1.3'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import deepxde\n",
    "\n",
    "deepxde.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cce409b",
   "metadata": {},
   "source": [
    "#### 设置deepxde.backend\n",
    "\n",
    "最好是保持后端为tensorflow.compat.v1，不要修改。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f2bc88e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !python -m deepxde.backend.set_default_backend tensorflow.compat.v1\n",
    "\n",
    "# !python -m deepxde.backend.set_default_backend tensorflow\n",
    "\n",
    "# !python -m deepxde.backend.set_default_backend pytorch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30ab5bdf",
   "metadata": {},
   "source": [
    "### GPU环境"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e8a40059",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "tf.config.list_physical_devices('GPU') # [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "73c5f3c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CUDA Available:  True\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "print(\"CUDA Available: \",torch.cuda.is_available()) # CUDA Available:  True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "321e1b2a",
   "metadata": {},
   "source": [
    "### CPU环境"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11a91bf4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "tf.config.list_physical_devices('GPU') # []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a3fb195",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "print(\"CUDA Available: \",torch.cuda.is_available()) # CUDA Available:  False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abc36b85",
   "metadata": {},
   "source": [
    "配置环境有可能遇到很多问题，大多是版本不兼容造成的。如果你安装失败了，请仔细检查包的版本。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17b68936",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py3.8",
   "language": "python",
   "name": "py3.8"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
